<template>
  <div class="ec-app-container">
    <!-- 查询条件 -->
    <div class="filter-container">
      <el-form label-width="90px">
        <el-row :gutter="10">
          <el-col :span="6">
            <el-form-item label="城市名称：" >
              <el-input v-model="listQuery.city" placeholder="姓名"/>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="是否可用：">
              <el-select v-model="listQuery.status" placeholder="发布状态" style="width: 100%;" clearable >
                <el-option v-for="(item, index) in GLOBAL.status" :key="index" :label="item" :value="index"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6" class="text-center">
            <el-button type="primary" class="border-4C7FD7 color-4C7FD7" icon="el-icon-search" @click="searchList">查询</el-button>
            <el-button class="border-4C7FD7 color-4C7FD7" @click="resetSearch">重置</el-button>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <!-- table -->
    <div>
      <div class="mt-30">
        <el-button class="filter-item" type="primary" @click="showDialog('新增区域')">新增</el-button>
      </div>
      <div class="mt-20">
        <el-table
          v-loading="listLoading"
          :data="list"
          border
          fit
          highlight-current-row
          style="width: 100%;"
          @selection-change="handleSelectionChange"
        >
          <el-table-column
            type="selection"
            width="55"/>
          <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button type="text" size="mini" @click="updateDialog(scope.row, '修改区域')">编辑</el-button>
              <el-button type="text" size="mini" @click="deleteDate(scope.row.id, '删除区域')">删除</el-button>
            </template>
          </el-table-column>
          <el-table-column label="城市名称" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.object.city | cityFilter }}</span>
            </template>
          </el-table-column>
          <el-table-column label="省份" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.object.province | provinceStatus }}</span>
            </template>
          </el-table-column>
          <el-table-column label="创建时间" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.createTime }}</span>
            </template>
          </el-table-column>
          <el-table-column label="更新时间" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.updateTime }}</span>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize" @pagination="searchList" />
    </div>
    <!-- 新增区域 -->
    <el-dialog :visible.sync="dialogFormVisible" :title="message" width="850px">
      <el-form
        ref="objectForm"
        :model="object"
        :rules="objectRules"
        label-width="110px">
        <el-row>
          <el-form-item
            label="图片："
            prop="pictureName">
            <single-image
              :length="1"
              @success="setImage"/>
          </el-form-item>
          <el-form-item
            label="省份："
            prop="pictureName">
            <el-select v-model="object.province" placeholder="省份" class="fl mr-7" style="width: 250px">
              <el-option
                v-for="province in provinces"
                :key="province.id"
                :label="province.name"
                :value="province.code"
              />
            </el-select>
          </el-form-item>
          <el-form-item
            label="城市名称："
            prop="pictureClassification">
            <el-select
              v-model="object.city"
              multiple
              filterable
              allow-create
              default-first-option
              placeholder="请选择城市名称">
              <el-option
                v-for="city in selectCity"
                :key="city.id"
                :label="city.name"
                :value="city.name"
              />
            </el-select>
          </el-form-item>
          <el-form-item
            label="邮政编码："
            prop="pictureClassification">
            <el-input v-model="object.postCode"/>
          </el-form-item>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="onCancel">取 消</el-button>
        <el-button :loading="btnLoading" type="primary" @click="onSubmit('新增分类')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination、
import SingleImage from '@/components/Upload/singleImage'
import { zmGetProvincesArr, zmGetCitiesArr } from '@/common/js/zmRegion'
import mixin from './rules/area.js'
export default {
  name: 'News',
  components: {
    Pagination,
    SingleImage
  },
  mixins: [mixin],
  data() {
    return {
      informationTypes: [],
      provinces: [],
      citys: [],
      listQuery: {
        level: 'area',
        city: '',
        status: '',
        pageSize: 10,
        pageNum: 1
      },
      object: {
        'level2': '',
        'city': [],
        'province': '',
        'postCode': '',
        'status': 0,
        'level': 'area',
        'level1': '',
        'picture': ''
      }
    }
  },
  computed: {
    selectCity() {
      return zmGetCitiesArr(this.object.province)
    }
  },
  created() {
    // 初始化
    this.searchList()
    this.provinces = zmGetProvincesArr()
  },
  methods: {
    // 提交
    setImage(file) {
      this.object.picture = file[0].imgUrl
    },
    onSubmit(message) {
      const object = this._.cloneDeep(this.object)
      object.city = this._.map(object.city, function(c) {
        return { name: c }
      })
      console.log(object)
      const objectData = {
        object: object,
        plateType: 'gnmpp_fangyuns'
      }
      this.saveNewMedia(objectData, message)
    }
  }
}
</script>
